package com.cmy.b_claculator.ui.add

import android.widget.Toast
import androidx.lifecycle.MediatorLiveData
import com.blankj.utilcode.util.TimeUtils
import com.cmy.b_claculator.db.ClaculatorDatabase
import com.cmy.b_claculator.db.dao.WorkDateDao
import com.cmy.b_claculator.db.entity.*
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import java.util.*

class AddWorkViewModel :BaseViewModel() {
    var id: Long = 0
    var numberStr= StringObservableField()//个数
    var unitStr: MediatorLiveData<String> =  MediatorLiveData()//单位
    var speciesStr: MediatorLiveData<String> =  MediatorLiveData()//
    var priceStr=StringObservableField()
    var timeStr=StringObservableField(TimeUtils.date2String( Date(),"yyyy-MM-dd"))//时间选择


    private  var dao :WorkDateDao = ClaculatorDatabase.getInstance(appContext).workDao

    /**选中单位*/
    var selecteUnitEntity :WorkUnitEntity? = null
    var  selecteSpeciesEntity :WorkSpeciesEntity? = null


   fun initData(){
       if (id > 0){
         val entity =  dao.queryWorkById(id)
           entity?.let {
               numberStr.set(it.number.toString())
               priceStr.set(it.price.toString())
               selecteSpeciesEntity =   dao.querySpeciesById(it.speciesId)
               speciesStr.value = selecteSpeciesEntity?.name

               selecteUnitEntity =   dao.queryUnitById(it.unitId)
               unitStr.value = selecteUnitEntity?.name
               timeStr.set(dao.queryWorkDateById(it.dataId)?.time)
           }


       }
    }

    fun selectUnitListPop(workUnitEntity: String) {
        unitStr.value = workUnitEntity
        selecteUnitEntity = dao.queryUnit(workUnitEntity)
    }
    fun selectSpeciesListPop(workSpecies: String) {
        speciesStr.value = workSpecies
        selecteSpeciesEntity = dao.querySpecies(workSpecies)
    }

    fun querySpecies(str:String): MutableList<String> {
        val list :MutableList<String> = mutableListOf()
        return try {
            val  workSpeciesList =dao.queryLikeSpecies(str)
            workSpeciesList?.let {
                for (element in it){
                    list.add(element = element.name)
                }
            }
            list
        }catch (e:Exception){
            e.printStackTrace()
            list
        }
    }

    fun queryUnit(str: String): MutableList<String> {
        val list :MutableList<String> = mutableListOf()
        return try {
            val  workSpeciesList =dao.queryLikeUnit(str)
            workSpeciesList?.let {
                for (element in it){
                    list.add(element = element.name)
                }
            }
            list
        }catch (e:Exception){
            e.printStackTrace()
            list
        }

    }




     fun submit(){
     var   speciesId = 0L
         if (selecteSpeciesEntity == null){
             speciesStr.value?.let {
                 selecteUnitEntity =  dao.queryUnit(it)
                 speciesId = if (selecteSpeciesEntity == null){
                     dao.insertWorkSpecies( WorkSpeciesEntity(it))
                 }else{
                     selecteSpeciesEntity!!.id
                 }
             }
         }else{
             speciesId = selecteSpeciesEntity!!.id
         }
         var unitId = 0L
         if (selecteUnitEntity == null){
             unitStr.value?.let {
                 selecteUnitEntity =  dao.queryUnit(it)

                 unitId = if (selecteUnitEntity == null){
                     dao.insertWorkUnit( WorkUnitEntity(it))
                 }else{
                     selecteUnitEntity!!.id
                 }
             }
         }else{
             unitId = selecteUnitEntity!!.id
         }

        val price =  priceStr.get().toDouble()
        val number =  numberStr.get().toInt()

        val dateEntity =  dao.queryWorkDate(timeStr.get())
            val dataId: Long
      if (dateEntity == null){
          dataId = dao.insertWorkDate(WorkDataEntity(timeStr.get(),0,price*number))
        }else{
            dataId = dateEntity.id
            val  money = (price*number)+dateEntity.money
            dao.upDateWorkDateMoney(WorkDataMoney(dataId,money))
        }
        val data = WorkEntity(speciesId, unitId,price,number, dataId,"")
         if (id > 0){
             data.id = id
         }
        dao.insertWork(data)
        Toast.makeText(appContext, "添加数据完成",Toast.LENGTH_SHORT).show()
    }





}